package com.example.kliven.because.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TabHost;
import android.widget.Toast;

import com.example.kliven.because.BaseActivity;
import com.example.kliven.because.R;
import com.example.kliven.because.fragment.DiscoverFragment;
import com.example.kliven.because.fragment.HomeFragment;
import com.example.kliven.because.fragment.MeFragment;
import com.example.kliven.because.fragment.MessageFragment;
import com.example.kliven.because.utils.DayNightMode;

import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.ShareSDK;


public class MainActivity extends BaseActivity implements View.OnClickListener {

    private TabHost mainTabHost;
    private FragmentManager fm;
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);
        DayNightMode.onActivityCreateSetTheme(this);
        setContentView(R.layout.activity_main);

        // 初始化JPush
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

        mainTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mainTabHost.setup();
        showTabHost();
    }

    private void showTabHost() {

        TabHost.TabSpec tab_homePage = mainTabHost.newTabSpec("tab_homePage");
        View view_homePage = getLayoutInflater().inflate(R.layout.item_tabhost_homepage, null);
        tab_homePage.setIndicator(view_homePage);
        tab_homePage.setContent(R.id.tabHost_homePage);
        mainTabHost.addTab(tab_homePage);


        TabHost.TabSpec tab_discover = mainTabHost.newTabSpec("tab_discover");
        View view_discover = getLayoutInflater().inflate(R.layout.item_tabhost_discover, null);
        tab_discover.setIndicator(view_discover);
        tab_discover.setContent(R.id.tabHost_discover);
        mainTabHost.addTab(tab_discover);

//        TabHost.TabSpec tab_camera = mainTabHost.newTabSpec("tab_camera");
//        View view_camera = getLayoutInflater().inflate(R.layout.item_tabhost_camera,null);
//        tab_discover.setIndicator(view_camera);
//        tab_discover.setContent(R.id.tabHost_camera);
//        mainTabHost.addTab(tab_homePage);

        TabHost.TabSpec tab_message = mainTabHost.newTabSpec("tab_message");
        View view_message = getLayoutInflater().inflate(R.layout.item_tabhost_message, null);
        tab_message.setIndicator(view_message);
        tab_message.setContent(R.id.tabHost_message);
        mainTabHost.addTab(tab_message);

        TabHost.TabSpec tab_me = mainTabHost.newTabSpec("tab_me");
        View view_me = getLayoutInflater().inflate(R.layout.item_tabhost_me, null);
        tab_me.setIndicator(view_me);
        tab_me.setContent(R.id.tabHost_me);
        mainTabHost.addTab(tab_me);

        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.tabHost_homePage, new HomeFragment());
        transaction.replace(R.id.tabHost_discover, new DiscoverFragment());
        transaction.replace(R.id.tabHost_message, new MessageFragment());
        transaction.replace(R.id.tabHost_me, new MeFragment());
        transaction.commit();

        mainTabHost.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.tabHost_camera:
//                Toast.makeText(this, "照相机被打开", Toast.LENGTH_SHORT).show();
//            break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ShareSDK.stopSDK(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // TODO Auto-generated method stub
//		return super.onKeyDown(keyCode, event);
        if (keyCode == KeyEvent.KEYCODE_BACK &&
                event.getAction() == KeyEvent.ACTION_DOWN) {

            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();

            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }
}
